package com.itcast.database.smooth.startup;

import com.itcast.database.smooth.entity.TradeOrder;
import com.itcast.database.smooth.service.DynamicShardingService;
import com.itcast.database.smooth.service.ITradeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>Description: </p>
 * @date 
 * @author 
 * @version 1.0
 * <p>Copyright:Copyright(c)2020</p>
 */
@SpringBootApplication
@ComponentScan(basePackages = {"com.itcast"})
@EnableTransactionManagement
@EnableConfigurationProperties(HibernateProperties.class)
@Controller
public class SmoothDatabaseApplication {

    @Autowired
    private DynamicShardingService dynamicShardingService;

    @Autowired
    private ITradeOrderService tradeOrderService;


    public static void main(String[] args) {
        SpringApplication.run(SmoothDatabaseApplication.class, args);
    }

    /**
     * 动态扩容调整
     * @param mod
     * @param response
     * @return
     */
    @GetMapping("/resize")
    public String resize(int mod, HttpServletResponse response) {
        try {
            dynamicShardingService.dynamicSharding(mod);
            response.getWriter().println("mod: " + mod);
        }catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 创建订单
     * @param accountNo
     * @param execPrice
     * @param response
     * @return
     */
    @GetMapping("/addOrder")
    public String addOrder(Long accountNo, Long execPrice, HttpServletResponse response) {
        try {
            Long id = tradeOrderService.addOrder(accountNo, execPrice);
            response.getWriter().println("add success, id: " + id);
        }catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据账户编号获取所有订单信息
     * @param response
     * @return
     */
    @GetMapping("/findAllByAccountNo")
    public String findAllByAccountNo(Long accountNo, HttpServletResponse response) {
        try {

            List<TradeOrder> tradeOrders = tradeOrderService.findAllByAccountNo(accountNo);
            if(null != tradeOrders && tradeOrders.size() > 0) {
                response.getWriter().println("Results: ");
                for(TradeOrder order : tradeOrders) {
                    response.getWriter().println(order);
                }
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取所有订单信息
     * @param response
     * @return
     */
    @GetMapping("/getAllOrders")
    public String getAllOrders(HttpServletResponse response) {
        try {

            List<TradeOrder> tradeOrders = tradeOrderService.getAllOrders();
            if(null != tradeOrders && tradeOrders.size() > 0) {
                response.getWriter().println("Results: ");
                for(TradeOrder order : tradeOrders) {
                    response.getWriter().println(order);
                }
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
